package com.micro.util;

import com.micro.model.KnowLib;
import com.micro.model.Subject;
import net.sf.json.JSONObject;
import okhttp3.*;
import okhttp3.Request.Builder;

import java.util.List;

public class DingMessageUtil {
    private static OkHttpClient client = new OkHttpClient();

    /**
     * 发送钉钉消息
     * @param title 标题
     * @param message 消息内容
     * @return
     */
    public static void sendToDingDing(Subject subject, String title, String message, List<KnowLib> knowLibList) {
        try {
            if (null != knowLibList && knowLibList.size() > 0) {
                for (int i = 0; i < knowLibList.size(); i++) {
                    KnowLib knowLibItem = knowLibList.get(i);
                    // 知识库比较
                    if (knowLibItem.getKnowCode().equals(subject.getSubjectCatName())) {
                        if (null != knowLibItem.getKnowDingding()) {
                            DingMessageUtil.sendMsg(title, message, knowLibItem.getKnowDingding());
                        }
                        if (null != knowLibItem.getKnowDingding2()) {
                            DingMessageUtil.sendMsg(title, message, knowLibItem.getKnowDingding2());
                        }
                    }
                }
            }
            if (null != subject.getKnowDingding()) {
                DingMessageUtil.sendMsg(title, message, subject.getKnowDingding());
            }
        } catch (Exception e) {
            e.printStackTrace();
            //logger.error("发送钉钉消息错误！ ", e);
        }
    }

    public static void sendMsg(String title, String message,String webhookUrl) {
        try {
            JSONObject text = new JSONObject();
            text.put("title", title);
            text.put("text", message);

            JSONObject json = new JSONObject();
            json.put("msgtype", "markdown");
            json.put("markdown", text);
            String jsonString = json.toString();

            String type = "application/json; charset=utf-8";
            RequestBody body = RequestBody.create(MediaType.parse(type), jsonString);
            if (null != webhookUrl && !"".equals(webhookUrl)) {
                Builder builder = new Request.Builder().url(webhookUrl);
                builder.addHeader("Content-Type", type).post(body);

                Request request = builder.build();
                Response response = client.newCall(request).execute();
                String resStr = response.body().string();
                System.out.println(String.format("send ding message:%s", resStr));
            }
        }  catch (Exception e) {
            e.printStackTrace();
            //logger.error("发送钉钉消息错误！ ", e);
        }
    }

    public static void main(String args[]) {
        String url = "https://oapi.dingtalk.com/robot/send?access_token=743c9c28a1287b204491a90c2745fa90bf0980f028fa21c97e68ef91206d20c8";
        String[] webhookArr = url.split(";");
        System.out.println(webhookArr[0]);
//        sendToDingDing("test", "test");
    }
}